Fix terminal escape sequence injection via HTTP response data#1817
Fix terminal escape sequence injection via HTTP response data#1817eddieran wants to merge 2 commits into
Conversation
Strip dangerous terminal control sequences (OSC, non-SGR CSI, C0 control chars) from HTTP response output when writing to a TTY. This prevents malicious servers from injecting escape codes that manipulate the terminal title, clipboard, cursor position, or display. Sanitization is only applied to TTY output — piped/redirected output is left untouched to preserve raw data for scripts. HTTPie's own SGR color sequences (ESC [ ... m) are preserved so syntax highlighting continues to work normally. Fixes httpie#1812
|
Heads-up on the red CI: the two failing jobs are I checked out the PR's base commit ( It's This PR's own additions ( |
Summary
Fixes #1812 — Terminal escape sequence injection via HTTP response data.
A malicious HTTP server can embed ANSI escape sequences in response headers or body to manipulate the user's terminal when HTTPie displays the output. This can be used to:
Fix
Added
httpie/output/sanitize.pywith asanitize_output()function that strips dangerous terminal control sequences from output bytes while preserving:\t,\n,\rESC [ ... m) used by HTTPie's own syntax highlightingThe sanitization is applied in
write_stream()andwrite_stream_with_colors_win()only when output is to a TTY (stdout_isatty=True). Piped/redirected output is left untouched so raw data is preserved for scripts.What is stripped
ESC ] 0;title BELESC [ 2 J,ESC [ 1;1 HESC cBEL,BS,VT,FF,DELFiles changed
httpie/output/sanitize.py— New module withsanitize_output()functionhttpie/output/writer.py— Apply sanitization when writing to TTYtests/test_terminal_sanitize.py— 19 tests (14 unit + 5 integration)Test plan
printf 'HTTP/1.1 200\r\nX-Evil: \x1b]0;pwned\x07\r\n\r\nbody' | nc -l 8888thenhttp localhost:8888— terminal title should not change